// pages/login/index.js
var App = getApp();
Page({

    data: {
        state: true, //登陆状态
        user: '' //用户信息
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        if (App.openid) {
            that.setData({
                state: false
            })
			that.sessionid();
            setTimeout(() => {
                wx.switchTab({
                    url: '../my/indexPage/index'
                })
            }, 1500)
        }
    },
    userInfo: function(res) {
        var that = this;
        wx.login({
            success: function(res) {
                console.log(res.code)
                if (res.code) {
					App.ajax.ajax('weixin/enter.do?method=wxaOpenid', {code:res.code},function(res) {
						if (res.data.jsonType == "success") {
						var openid = JSON.parse(res.data.result).openid;
						wx.setStorageSync('openid', openid);
							that.sessionid();
                            wx.navigateTo({
                                url: '../my/index/index',
                            })
                        } else {
                            wx.showToast({
                                title: '网络错误,请重试',
                                icon: 'none'
                            })
                        }
                    })
                } else {
                    wx.showToast({
                        title: '网络错误,请重试',
                        icon: 'none'
                    })
                }
            },
            fail: function() {
                wx.showToast({
                    title: '网络错误请重试',
                    icon: 'none'
                })
            }

        })
        that.setData({
            user: res.detail.userInfo
        })
        var imageUrl = res.detail.userInfo;
		wx.setStorageSync("info", imageUrl )
    },
    onReady: function() {
		
    },
	//获取sessionid
	sessionid:function(){
		App.ajax.ajax('login.do?method=loginWeb', {
			openid: wx.getStorageSync("openid"),
			name: wx.getStorageSync("info").nickName,
			img: wx.getStorageSync("info").avatarUrl,
		}, function (res) {
			console.log(res.data.data.JSESSIONID)
			wx.setStorageSync("JSESSIONID", res.data.data.JSESSIONID)
		})
	},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})